﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.DBService;
using MixScript.BLL;

namespace MixScript.BLL.WM
{
    public class cMOF:cBaseBLL
    {

        private string smofno;

        public string sMofno
        {
            get { return smofno; }
            set { smofno = value; }
        }
        private string ssection;

        public string sSection
        {
            get { return ssection; }
            set { ssection = value; }
        }
        private DateTime dtcreated;

        public DateTime dtCreated
        {
            get { return dtcreated; }
            set { dtcreated = value; }
        }


        public cMOF()
        {

        }

        public string getMOFno()
        {
            return new cDBHelper().genId("M", "MOFNO");
        }


         public bool getmoflist(ref DataTable dt, string spono, DateTime tgl1, DateTime tgl2, string stblname)
        {
            try
            {
                string ssql = "select * from " + stblname + " where 1=1  ";
                if (spono.Trim() != string.Empty)
                {
                    ssql += "and mofno='" + spono + "' ";
                }

                //if (scustid.Trim() != string.Empty)
                //{
                //    ssql += "and codfbpid='" + scustid + "' ";
                //}
                if (tgl1.Year > 2001 && tgl2.Year > 2001)
                {
                    ssql += "and dtcreated between '" + tgl1.ToString("yyyy-MM-dd") + "' and '" + tgl2.ToString("yyyy-MM-dd") + "'";
                }

                dt = Dbserver.ExecuteQuery(ssql, stblname);

                //  getInquiry(ref dt, new string[] { pono }, "vw_porpt", out s);
                return dt.Rows.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool saveMOF(DataTable dt,string sloginid,out string sout)
        {
            try
            {
                sout = "";
                DataTable dt2 = new DataTable();
                DataTable dtdtl = new DataTable();
                getTableStructure(ref dt2, "tb_mofmst");
                getTableStructure(ref dtdtl, "tb_mofdtl");
                
                sMofno = getMOFno() + "/" + getServerTime().ToString("MM-yyyy");
                foreach (DataRow drr in dt.Rows)
                {

                    if (drr["iorfno"].ToString() != string.Empty)
                    {
                        DataRow drdtl = dtdtl.NewRow();
                        //drdtl["delno"] = delno;
                        //drdtl["cuid"] = drr["cuid"];
                        drdtl["mofno"] = sMofno;
                        drdtl["iorfno"] = drr["iorfno"];
                        drdtl["mofdesc"] = drr["mofdesc"];
                        drdtl["lotno"] = drr["lotno"];

                        dtdtl.Rows.Add(drdtl);
                    }


                }



                DataRow dr = dt2.NewRow();
                //dr["delno"] = delno;
                dr["mofno"] = sMofno;
                dr["section"] = sSection;
                dr["idcreated"] = sloginid;
                dr["dtcreated"] = getServerTime();

                new cDBHelper().genIDCommit("M", "MOFNO");
                dt2.Rows.Add(dr);
                  int nret = 0;
                  if (dtdtl.Rows.Count > 0)
                  {
                      if (Dbserver.openConn())
                      {
                          nret = Dbserver.SqlUpdate(dt2, "tb_mofmst", out sout);
                          if (nret > 0)
                          {
                              nret = Dbserver.SqlUpdate(dtdtl, "tb_mofdtl", out sout);
                              Dbserver.CommitAndCloseConn();


                          }
                          else
                          {
                              Dbserver.ForceRollBackNClose();
                              return false;
                          }
                      }
                      else
                      {
                          sout = "Could not Open Database";
                          return false;

                      }
                      //Dbserver.SqlUpdate();

                      return nret > 0;
                  }
                  else
                  {
                      sout = "Detail is empty";
                      return false;
                  }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        
    }
}
